//配置通用的默认提示语
$.extend($.validator.messages, {
	required: '必填',
    equalTo: "请再次输入相同的值"
});

//校验数据库
function validateDatabaseDriverConfig($this, driverConfig){
	var isSuccess = true;
	var driver = $this.find("select[name='driver']").val();
	var username = $this.find("input[name='username']").val();
	var password = $this.find("input[name='password']").val();
	var table = $this.find("input[name='table']").val();
	var primaryKey = $this.find("input[name='primaryKey']").val().toLocaleUpperCase();;
	var url = $this.find("textarea[name='url']").val();
	
	driverConfig.driverClassName=driver;
	driverConfig.url=url;
	driverConfig.username=username;
	driverConfig.password=password;
	driverConfig.table=table;
	driverConfig.primaryKey=primaryKey;
	driverConfig.name=table;
	
	//对database表单进行必填校验
	var $required = "<label class='control-label formtips'>必填</label>";
	//如果是必填，则添加必填标识
	$this.find("input.required").each(function(){
		var $driverFormTips=$(this).next().hasClass('formtips');
		if($(this).val()!=""){
			return;
		}
		//如果没有必填标识则添加
		if(!$driverFormTips){
			$(this).css("border-color","#c71c1c");
			$(this).parent().append($required);
		}
		isSuccess=false;//只对表单进行必填校验
	});
	
	//文本失去焦点后
	$this.find("input.required").keyup(function(){
		//文本失去焦点后，去除必填标识
		$(this).parent().find(".formtips").remove();
		$(this).removeAttr("style");
		//如果值为空，再次添加必填标识
		if($(this).val()==""){
			$(this).css("border-color","#c71c1c");
			$(this).parent().append($required);
			isSuccess=false;//只对表单进行必填校验
		}
	});	
	
	//如果验证不成功
	if(!isSuccess)return null;
	
	//读取连接信息
	var result;
	$.ajax({  
		url : $basePath + "/driver/getDataSourceConnector",  
		contentType:"application/x-www-form-urlencoded",
	    type : "post",  
	    data : driverConfig,  
	    async : false,
	    success : function(data){
	    	isSuccess = data.success;
	    	if(isSuccess){
	    		result = data.resultValue;
	    	}else{
	    		bootGrowl(data.resultValue,"danger"); 
	    	}
	    }
    });

	//如果验证成功
	if(isSuccess && result!=null){
		var metaInfo = result.metaInfo;
		var cols = [];
		for (var i = 0; i < metaInfo.length; i++) {
			var col = metaInfo[i];
			cols.push({
				"name": col['ColumnName'],
				"typeName": col['ColumnTypeName'],
				"type": col['ColumnType']
			});
		}
		driverConfig.column = cols;
	}
	return result;
}